package com.example.consumer.service.strategy;

import com.example.common.Board;
import com.example.common.RandomUtils;

import java.util.*;

public class RowStrategy implements Strategy {
    @Override
    public int[] insertOneNumber(Board board, List<char[][]> allResult) {
        int[] rowCount = board.getRowCount();
        int min = Arrays.stream(rowCount)
                .min()
                .getAsInt();
        List<Integer> minRow = new ArrayList<>();
        for (int i = 0; i < rowCount.length; i++) {
            if (min == rowCount[i]) {
                minRow.add(i);
            }
        }
        //得到数字最少的一行（如有多行相同，随机选择一行）
        int minRowIndex = minRow.get(RandomUtils.getRandomInt(minRow.size()));
        char[][] board1 = board.getBoard();

        Map<String, Set<Character>> map = new HashMap<>();
        for (int j = 0; j < board1[0].length; j++) {
            if (board1[minRowIndex][j] == '.') {
                String s1 = minRowIndex + "@" + j;
                map.put(s1, new HashSet<>());
            }
        }
        for (char[][] result1 : allResult) {
            char[] chars = result1[minRowIndex];
            for (int j = 0; j < board1[0].length; j++) {
                if (board1[minRowIndex][j] == '.') {
                    String s1 = minRowIndex + "@" + j;
                    map.get(s1).add(chars[j]);
                }
            }
        }
        return chooseFromMap(board, map);
    }
}
